package com.ruoyi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.JwtUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import com.ruoyi.edu.domain.vo.EduTeacherVo;
import com.ruoyi.edu.mapper.EduCourseMapper;
import com.ruoyi.edu.mapper.EduTeacherMapper;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.mapper.SysOssMapper;
import com.ruoyi.user.domain.UcenterMember;
import com.ruoyi.user.domain.vo.UcenterMemberVo;
import com.ruoyi.user.mapper.UcenterMemberMapper;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edu.domain.bo.EduCommentBo;
import com.ruoyi.edu.domain.vo.EduCommentVo;
import com.ruoyi.edu.domain.EduComment;
import com.ruoyi.edu.mapper.EduCommentMapper;
import com.ruoyi.edu.service.IEduCommentService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 评论Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-22
 */
@RequiredArgsConstructor
@Service
public class EduCommentServiceImpl implements IEduCommentService {

   private final EduCommentMapper baseMapper;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Autowired
    private EduTeacherMapper eduTeacherMapper;
    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;
    @Autowired
    private SysOssMapper sysOssMapper;


    /**
     * 查询评论
     */
    @Override
    public EduCommentVo queryById(String id){
        return baseMapper.selectVoById(id);
    }




    /**
     * 查询评论列表
     */
    @Override
    public TableDataInfo<EduCommentVo> queryPageList(EduCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EduComment> lqw = buildQueryWrapper(bo);
        Page<EduCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<EduCommentVo> rows = result.getRecords();
        List<EduCommentVo> collect = rows.stream().map(item -> {
//            获取课程名称
            EduCourseVo eduCourseVo = eduCourseMapper.selectVoById(item.getCourseId());
            item.setTitle(eduCourseVo.getTitle());
//            获取教室名称
            EduTeacherVo eduTeacherVo = eduTeacherMapper.selectVoById(item.getTeacherId());
            item.setTeachername(eduTeacherVo.getName());
//            UcenterMemberVo ucenterMemberVo = ucenterMemberMapper.selectVoById(item.getMemberId());
//            item.setNickname(ucenterMemberVo.getNickname());
//            item.setAvatar(ucenterMemberVo.getAvatar());
            SysOss sysOss1 = sysOssMapper.selectById(item.getAvatar());
            item.setAvatar(String.valueOf(sysOss1.getUrl()));
            return item;
        }).collect(Collectors.toList());
        result.setRecords(collect);
        return TableDataInfo.build(result);
    }

    /**
     * 查询评论列表
     */
    @Override
    public List<EduCommentVo> queryList(EduCommentBo bo) {
        LambdaQueryWrapper<EduComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EduComment> buildQueryWrapper(EduCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EduComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCourseId()), EduComment::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherId()), EduComment::getTeacherId, bo.getTeacherId());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), EduComment::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), EduComment::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增评论
     */
    @Override
    public Boolean insertByBo(EduCommentBo bo) throws Exception {

        EduComment add = BeanUtil.toBean(bo, EduComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改评论
     */
    @Override
    public Boolean updateByBo(EduCommentBo bo) {
        EduComment update = BeanUtil.toBean(bo, EduComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EduComment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
